<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="退费日期" class="w-80 mr-5">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="zjm" label="" class="w-80">
          <a-input v-model:value="formState.zjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item name="pjhm" class="w-64 ml-5">
          <a-input v-model:value="formState.pjhm" placeholder="票据号" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <BaseExport type="门诊" name="退费检索" code="000061" is-show-time :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable fill :loading="loading" column-code="000061" :scroll="{ x: 2600 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'no'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'pjhm'">
            <PopoverTable
              :config="detailConfig"
              :params="{
                stdate: formState.dateRange?.[0],
                eddate: formState.dateRange?.[1],
                zjm: formState.zjm,
                pjhm: record.pjhm,
                sftfbz: '3'
              }"
              :width="1000"
            >
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'mc'">
                  <MedicineInfo :value="{ mc: popRecord.mc, gg: popRecord.gg }" />
                </template>
              </template>
              {{ record.pjhm }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'yspjhm'">
            <PopoverTable
              :config="detailConfig"
              :params="{
                stdate: formState.dateRange?.[0],
                eddate: formState.dateRange?.[1],
                zjm: formState.zjm,
                pjhm: record.yspjhm,
                sftfbz: '3'
              }"
              :width="1000"
            >
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'mc'">
                  <MedicineInfo :value="{ mc: popRecord.mc, gg: popRecord.gg }" />
                </template>
              </template>
              {{ record.yspjhm }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrint(record)"> 补打收据 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'

const formRef = ref(null)
const formState = reactive({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  zjm: '',
  pjhm: '',
  sftfbz: '3'
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/oupCharge/getOuptRefundList',
  formatParams: () => {
    return {
      stdate: formState.dateRange?.[0],
      eddate: formState.dateRange?.[1],
      zjm: formState.zjm,
      pjhm: formState.pjhm,
      sftfbz: '3'
    }
  }
})
const detailConfig = ref({
  url: '/outp/oupCharge/getChargeDetailList',
  columnCode: '000062',
  rowKey: 'no',
  formatResult: (data) => {
    return data.map((item, index) => ({
      ...item,
      no: index + 1
    }))
  }
})
const onPrint = async (row) => {
  if (row.jslxbz === 1) {
    //挂号收费
    const { code, msg, data } = await post('/outp/outPrint/getRegistrationBillPrint', { jzbh: row.jzbh, pjhm: row.pjhm, sftfbs: '1' })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  } else {
    //门诊收费
    const { code, msg, data } = await post('/outp/oupCharge/PrintMedicareCharges', { jzbh: row.jzbh, pjhm: row.pjhm, sftfbs: '1' })
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }
}

onMounted(() => {
  getData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
